/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2014-7-5         admin
 */

package com.tianci.appstore.ui.search;

import android.content.Context;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.coocaa.x.framework.utils.LogUtils;
import com.tianci.appstore.R;
import com.tianci.appstore.ui.search.SearchKeyItemView.OnItemClickListener;
import com.tianci.appstore.utils.SkyConstants;

import java.util.ArrayList;

/**
 * @author admin
 * @ClassName SearchT9Layout
 * @date 2014-7-5
 */
public class SearchT9KeyLayout extends SearchInputLayout implements OnItemClickListener {
    private Context mContext;
    private Integer[] keyIDs = {R.string.search_key_1, R.string.search_t9_key_abc2,
            R.string.search_t9_key_abc3, R.string.search_t9_key_abc4, R.string.search_t9_key_abc5,
            R.string.search_t9_key_abc6, R.string.search_t9_key_abc7, R.string.search_t9_key_abc8,
            R.string.search_t9_key_abc9};
    private ArrayList<SearchKeyItemView> keyItems;

    private Button num_0Btn;

    private LinearLayout deletLayout, clearLayout;
    private ImageView deletIconView, clearIconView;
    private TextView deletTextView, clearTextView;

    private LinearLayout t9_item_contetLayout;
    private LinearLayout textLayout;
    private ArrayList<TextView> t9Texts;
    private FrameLayout.LayoutParams t9_item_content_p;
    private int padding = 0;
    private Button mT9Btn, mNormalBtn;
    private SearchKeyItemView onClickView;

    private OnT9InputListener inpListener;

    public interface OnT9InputListener {
        public void inputEvent_t9(String str);

        public void deletEvent_t9();

        public void clearEvent_t9();
    }

    public SearchT9KeyLayout(Context context) {
        super(context);
        mContext = context;
    }

    public void setOnT9InputListener(OnT9InputListener listener) {
        inpListener = listener;
    }

    public void setButtons(Button t9, Button normal) {
        this.mT9Btn = t9;
        this.mNormalBtn = normal;
    }

    private void initBtnView() {
        deletLayout = new LinearLayout(mContext);
        deletLayout.setFocusable(true);
        setFocusChangeEvent(deletLayout);
        deletLayout.setTag("delete");
        deletLayout.setOnClickListener(btnClickListener);
        deletLayout.setOnKeyListener(btnOkeyListener);
        deletLayout.setGravity(Gravity.CENTER);
        deletLayout.setOrientation(LinearLayout.HORIZONTAL);
        deletLayout.setBackgroundResource(mItemBackId);
        clearLayout = new LinearLayout(mContext);
        clearLayout.setFocusable(true);
        setFocusChangeEvent(clearLayout);
        clearLayout.setTag("clear");
        clearLayout.setOnClickListener(btnClickListener);
        clearLayout.setOnKeyListener(btnOkeyListener);
        clearLayout.setGravity(Gravity.CENTER);
        clearLayout.setOrientation(LinearLayout.HORIZONTAL);
        clearLayout.setBackgroundResource(mItemBackId);

        deletIconView = new ImageView(mContext);
        deletIconView.setImageResource(R.drawable.search_delete);
        clearIconView = new ImageView(mContext);
        clearIconView.setImageResource(R.drawable.search_clear);

        deletTextView = new TextView(mContext);
        deletTextView.setText(R.string.search_delete);
        deletTextView.setTextColor(0xfffffefe);
        deletTextView.setTextSize(SkyConstants.Dpi(40));
        clearTextView = new TextView(mContext);
        clearTextView.setText(R.string.search_clear);
        clearTextView.setTextColor(0xfffffefe);
        clearTextView.setTextSize(SkyConstants.Dpi(40));

        deletLayout.addView(deletIconView);
        LinearLayout.LayoutParams de_p = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        de_p.leftMargin = SkyConstants.Div(16);
        deletLayout.addView(deletTextView, de_p);
        clearLayout.addView(clearIconView);
        clearLayout.addView(clearTextView);
    }

    public void initView() {
        initBtnView();
        metroKeyLayout = new SearchKeyLayout(mContext);
        num_0Btn = new Button(mContext);
        num_0Btn.setText(R.string.search_key_0);
        num_0Btn.setTag("num_0");
        num_0Btn.setOnKeyListener(btnOkeyListener);
        num_0Btn.setOnClickListener(btnClickListener);
        num_0Btn.setBackgroundResource(mItemBackId);
        num_0Btn.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    num_0Btn.setTextColor(0xffffffff);
                    num_0Btn.getPaint().setFakeBoldText(true);
                } else {
                    num_0Btn.setTextColor(0xffb2b2b2);
                    num_0Btn.getPaint().setFakeBoldText(false);
                }
            }
        });
        setFocusChangeEvent(num_0Btn, num_0Btn.getOnFocusChangeListener());
        num_0Btn.setTextColor(0xffb2b2b2);
        num_0Btn.setTextSize(SkyConstants.Dpi(40));
        FrameLayout.LayoutParams del_p = new FrameLayout.LayoutParams(
                SkyConstants.Div(289), SkyConstants.Div(180));
        del_p.leftMargin = SkyConstants.Div(94 - 40);
        this.addView(deletLayout, del_p);
        FrameLayout.LayoutParams num_p = new FrameLayout.LayoutParams(
                SkyConstants.Div(289), SkyConstants.Div(180));
        num_p.leftMargin = SkyConstants.Div(258);
        this.addView(num_0Btn, num_p);
        FrameLayout.LayoutParams cle_p = new FrameLayout.LayoutParams(
                SkyConstants.Div(289), SkyConstants.Div(180));
        cle_p.leftMargin = SkyConstants.Div(462);
        this.addView(clearLayout, cle_p);
        FrameLayout.LayoutParams key_p = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);
        key_p.leftMargin = SkyConstants.Div(94 - 40);
        key_p.topMargin = SkyConstants.Div(114);
        this.addView(metroKeyLayout, key_p);
        setKeyLayout();

        t9_item_contetLayout = new LinearLayout(mContext);
        t9_item_contetLayout.setFocusable(true);
        if (mFocusChangedEvent != null) {
            t9_item_contetLayout.setBackgroundResource(R.drawable.game_center_search_second);
        } else {
            t9_item_contetLayout.setBackgroundResource(R.drawable.search_second);
        }
        setFocusChangeEvent(t9_item_contetLayout);
        t9_item_contetLayout.setOrientation(LinearLayout.HORIZONTAL);
        t9_item_contetLayout.setGravity(Gravity.CENTER);
        t9_item_contetLayout.setVisibility(View.INVISIBLE);
        t9Texts = new ArrayList<TextView>();
        t9_item_content_p = new FrameLayout.LayoutParams(SkyConstants.Div(380),
                SkyConstants.Div(175));
        t9_item_contetLayout.setLayoutParams(t9_item_content_p);
        this.addView(t9_item_contetLayout);

        textLayout = new LinearLayout(mContext);
        textLayout.setOrientation(LinearLayout.HORIZONTAL);
        textLayout.setGravity(Gravity.CENTER);
    }

    public void setDeleteLayoutFocus() {
        deletLayout.requestFocus();
    }

    public void setClearLayoutFocus() {
        clearLayout.requestFocus();
    }

    OnClickListener btnClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v.getTag().equals("delete")) {
                if (inpListener != null)
                    inpListener.deletEvent_t9();
            } else if (v.getTag().equals("clear")) {
                if (inpListener != null)
                    inpListener.clearEvent_t9();
            } else if (v.getTag().equals("num_0")) {
                if (inpListener != null && num_0Btn != null && num_0Btn.getText() != null)
                    inpListener.inputEvent_t9(num_0Btn.getText().toString());
            }
        }
    };

    private void setKeyLayout() {
        int size = keyIDs.length;
        keyItems = new ArrayList<SearchKeyItemView>();
        metroKeyLayout.setSize(3, 2);
        for (int i = 0; i < size; i++) {
            SearchKeyItemView keyItem = new SearchKeyItemView(mContext, -1,
                    SearchKeyItemView.IS_T9, SkyConstants.Div(209),
                    SkyConstants.Div(181));
            setFocusChangeEvent(keyItem, keyItem.getOnFocusChangeListener());
            keyItem.setBackgroundResource(mItemBackId);
            keyItem.setOnItemClickListener(this);
            if (i < 3) {
                metroKeyLayout.addSubView(keyItem, 0, i);
            } else if (i >= 3 && i < 6) {
                metroKeyLayout.addSubView(keyItem, 1, i - 3);
            } else if (i >= 6 && i < 9) {
                metroKeyLayout.addSubView(keyItem, 2, i - 6);
            }
            keyItems.add(keyItem);
            keyItem.setTextValue(keyIDs[i]);
            keyItem.setOnKeyListener(itemOnKeyListener);
        }
    }

    private void setSubT9TextAttribute(final SearchKeyItemView view, int textSize, int textColor) {
        t9_item_contetLayout.removeAllViews();
        textLayout.removeAllViews();
        t9Texts.clear();
        String value = view.keyText.getText().toString().replace(" ", "");
        int t9Size = value.length();
        if (t9Size == 4) {
            padding = SkyConstants.Div(23);
        } else if (t9Size == 5) {
            padding = SkyConstants.Div(15);
        }
        for (int i = 0; i < t9Size; i++) {
            TextView t9 = new TextView(mContext);
            t9.setPadding(padding, 0, padding, 0);
            t9.setTextSize(textSize);
            t9.setFocusable(true);
            t9.setTextColor(textColor);
            t9.setOnClickListener(t9ClickListener);
            t9.setOnKeyListener(t9OnkeyListener);
            t9.setOnFocusChangeListener(new OnFocusChangeListener() {
                @Override
                public void onFocusChange(View view, boolean focused) {
                    view.setPadding(padding, 0, padding, 0);
                    TextView v = (TextView) view;
                    if (focused) {
                        v.setTextColor(0xff3598dc);
                        v.getPaint().setFakeBoldText(true);
                    } else {
                        v.setTextColor(0xffffffff);
                        v.getPaint().setFakeBoldText(false);
                    }
                }
            });
            String s = value.substring(i, i + 1);
            t9.setText(s);
            t9.setId(100 + i);
            t9.setTag(s);
            t9Texts.add(t9);
            textLayout.addView(t9);
        }
        t9_item_contetLayout.addView(
                textLayout,
                new FrameLayout.LayoutParams(SkyConstants.Div(354), SkyConstants.Div(105)));

        t9_item_contetLayout.post(new Runnable() {
            @Override
            public void run() {
                t9_item_content_p.leftMargin = view.getXPostion()
                        + (view.getWidthSize() - SkyConstants.Div(354 + 110)) / 2
                        + SkyConstants.Div(94);
                t9_item_content_p.topMargin = view.getYPostion()
                        + (view.getHeightSize() - SkyConstants.Div(135)) / 2
                        + SkyConstants.Div(98);
                t9_item_contetLayout.setLayoutParams(t9_item_content_p);
                LogUtils.d("search", "focused t9_item_content_p.leftMargin == "
                        + t9_item_content_p.leftMargin);
                LogUtils.d("search", "focused t9_item_content_p.topMargin == "
                        + t9_item_content_p.topMargin);
            }
        });
    }

    OnKeyListener btnOkeyListener = new OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                if (v.getTag().equals("delete")) {
                    if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
                        clearLayout.requestFocus();
                        return true;
                    }
                    if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
                        mT9Btn.requestFocus();
                        return true;
                    }
                } else if (v.getTag().equals("clear")) {
                    // 按键焦点处理
                    if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
                        mNormalBtn.requestFocus();
                        return true;
                    } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
                        if (mClearFocusListener != null) {
                            return mClearFocusListener.clearFocus(keyCode);
                        }
                    }
                } else if (v.getTag().equals("num_0")) {
                    if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
                        mT9Btn.requestFocus();
                        return true;
                    }
                }
            }
            return false;
        }
    };

    OnKeyListener t9OnkeyListener = new OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                switch (keyCode) {
                    case KeyEvent.KEYCODE_DPAD_UP:
                    case KeyEvent.KEYCODE_DPAD_DOWN:
                    case KeyEvent.KEYCODE_BACK:
                        t9_item_contetLayout.setVisibility(View.INVISIBLE);
                        onClickView.setFocusable(true);
                        onClickView.requestFocus();
                        onClickView.setBackgroundResource(mItemBackId);
                        return true;

                    case KeyEvent.KEYCODE_DPAD_LEFT:
                        if (v.getId() == 100) {
                            t9Texts.get(t9Texts.size() - 1).requestFocus();
                            return true;
                        } else {
                            t9Texts.get(v.getId() - 100 - 1).requestFocus();
                            return true;
                        }
                    case KeyEvent.KEYCODE_DPAD_RIGHT:
                        if (v.getId() == 100 + (t9Texts.size() - 1)) {
                            t9Texts.get(0).requestFocus();
                            return true;
                        } else {
                            t9Texts.get(v.getId() - 100 + 1).requestFocus();
                            return true;
                        }

                    default:
                        break;
                }
            }
            return false;
        }
    };

    OnClickListener t9ClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (inpListener != null)
                inpListener.inputEvent_t9(v.getTag().toString());
            t9_item_contetLayout.setVisibility(View.INVISIBLE);
            onClickView.setFocusable(true);
            onClickView.requestFocus();
        }
    };

    OnKeyListener itemOnKeyListener = new OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                switch (keyCode) {
                    case KeyEvent.KEYCODE_DPAD_LEFT:
                        int id = v.getId();
                        if (id == 0 || id == 10 || id == 20) {
                            metroKeyLayout.setFocusItem(id + 2);
                            return true;
                        }
                        return false;
                    case KeyEvent.KEYCODE_DPAD_RIGHT:
                        int id1 = v.getId();
                        if (mClearFocusListener != null && (id1 == 2 || id1 == 12 || id1 == 22)) {
                            return mClearFocusListener.clearFocus(keyCode);
                        }
                        break;
                    default:
                        break;
                }
            }
            return false;
        }
    };

    @Override
    public void itemClickEvent(String keyValue) {

    }

    @Override
    public void t9_itemClickEvent(SearchKeyItemView view) {
        if (view.getId() != 0) {
            t9_item_contetLayout.setLayoutParams(t9_item_content_p);
            onClickView = view;
            t9_item_contetLayout.setVisibility(View.VISIBLE);
            t9_item_contetLayout.requestFocus();
            if (t9Texts.size() > 0) {
                t9Texts.get(0).requestFocus();
            }
            onClickView.setFocusable(false);
            onClickView.setBackgroundResource(mItemBackId);
        } else {
            if (inpListener != null && view.keyText != null && view.keyText.getText() != null)
                inpListener.inputEvent_t9(view.keyText.getText().toString());
        }
    }

    @Override
    public void onItemFocusEvent(SearchKeyItemView view) {
        if (view.getId() != 0)
            setSubT9TextAttribute(view, SkyConstants.Dpi(50), 0xffffffff);
    }
}
